from django.shortcuts import render

from django.http import JsonResponse
# Create your views here.

from rest_framework.viewsets import GenericViewSet

from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from . import serializer
from . import models


class CategoryView(GenericViewSet, ListModelMixin):
    queryset = models.CourseCategory.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = serializer.CourseCategorySerializer


from .paginations import PageNumberPagination
from rest_framework.filters import OrderingFilter, SearchFilter
from .filters import MyFilter
from django_filters.rest_framework import DjangoFilterBackend
from .SerachBackend import COurseFilterSet

#课程详情接口
class CouresView(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = serializer.CourseSerializer
    pagination_class = PageNumberPagination

    # 过滤和排序
    # filter_backends=[DjangoFilterBackend,OrderingFilter,SearchFilter]
    # filter_backends=[DjangoFilterBackend,OrderingFilter,MyFilter]
    #它两要一起用
    filter_backends = [OrderingFilter, DjangoFilterBackend]
    filter_class = COurseFilterSet
    # filter_backends=OrderingFilter
    # 允许排序的字段
    ordering_fields = ['id', 'price', 'students']
    # search_fields=['course_category']
    # 允许过滤的字段
    filter_fields = ['course_category', 'students']


# search搜索
# 允许搜索(内置搜索，只能按照search='name或者id或者brief)
# 想要实现更高级的搜索，需要自己写
# 基于django-filter扩写
# 纯自己写
# search_fields=['name','id','brief']


# 课程章节接口
# 查多条
class ChapterViewSet(GenericViewSet, ListModelMixin):
    queryset = models.CourseChapter.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = serializer.CourseChapterSerializer

    #加上过滤功能，按照课程的id号来过滤
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']



class CourserSearchView(GenericViewSet,ListModelMixin):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('-orders')
    serializer_class =serializer.CourseSerializer
    #内置搜索
    filter_backends = [SearchFilter]
    search_fields = ['name','brief']
    pagination_class = PageNumberPagination









